package com.example.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @Author：wlz
 * @Package：com.example.es
 * @Project：redis
 * @name：ESTest_Index_Create
 * @Date：2023/4/15 22:18
 * @Filename：ESTest_Index_Create
 */
public class ESTest_Doc_Query {
    public static void main(String[] args) throws IOException {
        //创建
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200)));
        //查询数据
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//
//        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
//
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = index.getHits();
//
//                //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//             ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",15)));
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //分页查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        query.from(2);
//        query.size(2);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //查询排序
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        query.sort("age", SortOrder.ASC);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //过滤字段
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        String[] ex = {};
//        String[] inc = {"name"};
//        query.fetchSource(inc,ex);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //组合查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age",15));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","lisi"));
//        query.query(boolQueryBuilder);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //范围查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder();
//        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//        rangeQuery.gte(15);
//        query.query(rangeQuery);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }
        //模糊查询

//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder();
//        FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "lisi").fuzziness(Fuzziness.ONE);
//        query.query(fuzziness);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //高亮查询

//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder();
//        TermsQueryBuilder termQueryBuilder = QueryBuilders.termsQuery("name", "lisi");
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        highlightBuilder.preTags("<font color = 'red'>");
//        highlightBuilder.postTags("</font>");
//        highlightBuilder.field("name");
//
//        query.highlighter(highlightBuilder);
//        query.query(termQueryBuilder);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        //聚合查询
//        SearchRequest request = new SearchRequest();
//        request.indices("user1");
//        SearchSourceBuilder query = new SearchSourceBuilder();
//        MaxAggregationBuilder maxAge = AggregationBuilders.max("maxAge").field("age");
//        query.aggregation(maxAge);
//        request.source(query);
//        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = index.getHits();
//
//        //响应状态
//        System.out.println(hits.getTotalHits());
//        System.out.println(index.getTook());
//
//        for (SearchHit hit: hits
//        ) {
//            System.out.println(hit.getSourceAsString());
//
//        }

        SearchRequest request = new SearchRequest();
        request.indices("user1");
        SearchSourceBuilder query = new SearchSourceBuilder();
        TermsAggregationBuilder field = AggregationBuilders.terms("ageGroup").field("age");
        query.aggregation(field);
        request.source(query);
        SearchResponse index = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = index.getHits();

        //响应状态
        System.out.println(hits.getTotalHits());
        System.out.println(index.getTook());
        System.out.println(index.getInternalResponse());

        for (SearchHit hit: hits
        ) {
            System.out.println(hit.getSourceAsString());

        }

        //关闭
        client.close();
    }
}
